package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.setting.eneity.User;
import com.bjpowernode.crm.util.DateTimeUtil;
import com.bjpowernode.crm.util.UUIDUtil;
import com.bjpowernode.crm.workbench.eneity.Activity;
import com.bjpowernode.crm.workbench.eneity.ActivityRemark;
import com.bjpowernode.crm.workbench.exception.ActivityException;
import com.bjpowernode.crm.workbench.service.ActivityRemarkService;
import com.bjpowernode.crm.workbench.service.ActivityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ActivityRemarkController {
    @Resource
    private ActivityRemarkService remarkService;
    @ResponseBody
    @RequestMapping(value = "/workbench/activityRemark/query.do")
    public List<ActivityRemark> queryList(String activityId){
        //调用service层
        List<ActivityRemark> remarkList=remarkService.queryRemarks(activityId);
        return remarkList;
    }
    @ResponseBody
    @RequestMapping(value = "/workbench/activityRemark/insert.do")
    public Map<String,Object> insert(ActivityRemark remark){
        //完善备注对象
        remark.setId(UUIDUtil.getUUID());
        remark.setEditFlag("0");
        remark.setCreateTime(DateTimeUtil.getSysTime());
        //调用service层
        Boolean success= null;
        try {
            success = remarkService.insertRemark(remark);
        } catch (ActivityException e) {
            e.printStackTrace();
        }finally {
            //将结果写入map
            Map<String,Object> map=new HashMap<>();
            map.put("success",success);
            map.put("data",remark);
            return map;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/workbench/activityRemark/remove.do")
    public Map<String,Boolean> remove(String id){
        //调用service对象
        Boolean success= null;
        Map<String,Boolean> map=new HashMap<>();
        try {
            success = remarkService.remove(id);
        } catch (ActivityException e) {
            e.printStackTrace();
        }finally {
            //将结果写入map
            map.put("success",success);
            return map;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/workbench/activityRemark/update.do")
    public Map<String,Boolean> update(HttpServletRequest request,ActivityRemark remark){
        User user= (User) request.getSession(false).getAttribute("loginUser");
        remark.setEditFlag("1");
        remark.setEditBy(user.getName());
        remark.setEditTime(DateTimeUtil.getSysTime());
        //调用service
        Boolean success= null;
        try {
            success = remarkService.update(remark);
        } catch (ActivityException e) {
            e.printStackTrace();
        }
        Map<String,Boolean> map=new HashMap<>();
        map.put("success",success);
        return map;
    }
}
